package com.shujia.common.util

import java.text.SimpleDateFormat
import java.util.Date

import org.apache.spark.sql.SparkSession

abstract class SparkTool {

  var day_id: String = _
  var month_id: String = _

  def main(args: Array[String]): Unit = {
    if (args.length == 0) {
      println("请指定时间参数")
      return
    }

    day_id = args(0)

    if ("%y%m%d".equals(day_id)) {
      val f: SimpleDateFormat = new SimpleDateFormat("yyyyMMdd")
      day_id = f.format(new Date)
    }
    println("当前分区时间：" + day_id)


    month_id = day_id.substring(0, 6)
    println("当前月分区：" + month_id)


    val appName: String = this.getClass.getSimpleName.replace("$", "")


    val spark: SparkSession = SparkSession
      .builder()
      .appName(appName)
      .enableHiveSupport() //使用hived的元数据
      .getOrCreate()



    //计算两个网格点距离的函数
    spark
      .udf
      .register(
        "calculateLength",
        (grid1: String, grid2: String) => Geography.calculateLength(grid1.toLong, grid2.toLong)
      )



    //调用子类实现的run方法

    this.run(spark)

  }

  //在子类中编写具体的逻辑
  def run(spark: SparkSession): Unit

}
